﻿package application
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	/**
	 * Preloader
	 * @author 
	 */
	[ SWF( backgroundColor = '#467cbd', width = '800', height = '600', frameRate='60' ) ]
	public class Preloader extends MovieClip 
	{
		private var tf:TextField;
		
		public function Preloader()
		{
			addEventListener(Event.ENTER_FRAME, checkFrame);
			loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
			// show loader
			tf = new TextField()
			tf.defaultTextFormat = new TextFormat("Verdana", 144, 0xFFFFFF, true);
			tf.x = 100
			tf.y = 50
			tf.width = 600
			tf.height = 600
			addChild(tf);
		}
		
		private function progress(e:ProgressEvent):void 
		{
			// update loader
			tf.text = int((e.bytesLoaded / e.bytesTotal)*100) + "%";
		}
		
		private function checkFrame(e:Event):void 
		{
			if (currentFrame == totalFrames) 
			{
				removeEventListener(Event.ENTER_FRAME, checkFrame);
				startup();
			}
		}
		
		private function startup():void 
		{
			// hide loader
			removeChild(tf);
			stop();
			loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
			var mainClass:Class = getDefinitionByName("application.Main") as Class;
			addChild(new mainClass() as DisplayObject);
		}
		
	}
	
}